package com.hsyt.pms.controller.system;

import com.alibaba.dubbo.config.annotation.Reference;
import com.hsyt.pfc.support.rpc.PageResult;
import com.hsyt.pfc.support.util.EnumToDataUtil;
import com.hsyt.pfc.support.util.LoggerUtil;
import com.hsyt.pms.api.common.PltAuth;
import com.hsyt.pms.api.common.PltLog;
import com.hsyt.pms.api.common.ResultMessage;
import com.hsyt.pms.api.dto.system.SysRoleDto;
import com.hsyt.pms.api.enums.AbleStatus;
import com.hsyt.pms.api.vo.system.SysRoleVo;
import com.hsyt.pms.controller.base.BaseController;
import com.hsyt.pms.sys.dubbo.SysRoleService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.MessageFormat;
import java.util.List;

/**
 * @ClassName: SysRoleController
 * @Description: 角色服务控制层
 * @author: wangze@hsyuntai.com
 * @date: 2019/1/29 18:19
 */
@RestController
@RequestMapping("/pms/system/role")
public class SysRoleController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysRoleController.class);

    @Reference(group = "pmsSystem", registry = "pms", version = "0.0.1")
    private SysRoleService pmsRoleService;

    @RequestMapping(value = "/page")
    @PltAuth(auth ="ROLE_ROLE_MG")
    public ModelAndView page(ModelMap model) {
        String statusComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(AbleStatus.class);
        model.put("statusComboData", statusComboData);
        return new ModelAndView("system/pageRole");
    }
    @RequestMapping(value = "/toEditForm")
    @PltAuth(auth ="ROLE_ROLE_MG_ADD,ROLE_ROLE_MG_MODIFY")
    public ModelAndView toEditForm(ModelMap model, @RequestParam("formType") String formType) {
        model.put("formType", formType);
        return new ModelAndView("system/formRole");
    }

    @PltLog(log = "分页查询角色列表")
    @PltAuth(auth ="ROLE_ROLE_MG")
    @RequestMapping(value = "/findPageData")
    public @ResponseBody
    PageResult findPageData(@RequestBody SysRoleDto dto) {
        PageResult<SysRoleVo> pageResult = new PageResult<SysRoleVo>();
        try {
            pageResult = pmsRoleService.searchPageDataByDto(dto);
        } catch (Exception e) {
            pageResult.setResult(false);
            String message = MessageFormat.format("SysRoleController.searchPageData error: {0}", e.getMessage());
            pageResult.setMessage(message);
            LoggerUtil.error(LOGGER, message);
        }
        return pageResult;
    }

    @PltLog(log = "保存角色")
    @PltAuth(auth ="ROLE_ROLE_MG_ADD,ROLE_ROLE_MG_MODIFY")
    @RequestMapping(value = "/save")
    public @ResponseBody
    ResultMessage save(@RequestBody SysRoleDto role, String formType, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "成功");
        try {
            if (role.getId() == null) {
                if (pmsRoleService.countRole(role) > 0) {
                    result.change(false, "角色名已存在!");
                    return result;
                }
                if (StringUtils.isBlank(role.getStatus())) {
                    role.setStatus(AbleStatus.ENABLED.getCode());
                }
                pmsRoleService.addRole(role);
            } else {
                pmsRoleService.updateRoleByKeySelective(role);
            }
        } catch (Exception e) {
            result.change(false, "保存失败 , 错误信息:" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysRoleController-save error: {0}", e.getMessage());
        }
        return result;
    }

    @PltLog(log = "保存角色菜单")
    @RequestMapping(value = "/saveMenus")
    public @ResponseBody
    ResultMessage saveMenus(@RequestBody SysRoleDto SysRoleDto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "成功");
        try {
            pmsRoleService.saveMenus(SysRoleDto);
        } catch (Exception e) {
            result.change(false, "保存失败,错误信息:" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysRoleController-saveMenus error: {0}", e.getMessage());
        }
        return result;
    }

    @PltLog(log = "更改角色启用/停用状态")
    @RequestMapping(value = "/doUndo")
    public @ResponseBody
    ResultMessage doUndo(@RequestBody SysRoleDto role, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "成功");
        if (role.getId() == null) {
            result.change(false, "参数中角色id为空");
            return result;
        }
        if (StringUtils.isBlank(role.getStatus())) {
            role.setStatus(AbleStatus.ENABLED.getCode());
        }
        try {
            pmsRoleService.updateRoleByKeySelective(role);
        } catch (Exception e) {
            result.change(false, "操作失败,错误信息:" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysRoleController doUndo error: {0}", e.getMessage());
        }
        return result;
    }

    @PltLog(log = "删除角色")
    @PltAuth(auth ="ROLE_ROLE_MG_DELETE")
    @RequestMapping(value = "/deleteRole")
    public @ResponseBody
    ResultMessage deleteRole(@RequestBody SysRoleDto role, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "删除成功");
        if (role.getId() == null) {
            result.change(false, "参数中角色id为空");
            return result;
        }
        role.setStatus(AbleStatus.DISABLED.getCode());
        try {
            pmsRoleService.updateRoleByKeySelective(role);
        } catch (Exception e) {
            result.change(false, "删除失败,错误信息:" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysRoleController deleteRole error: {0}", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/searchRoleByOrg")
    public @ResponseBody
    ResultMessage searchRoleByOrg(@RequestParam Long orgId, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "成功");
        try {
            if(orgId == null) {
                result.change(false, "获取失败,错误信息: orgId为空!");
                return result;
            }
            List<SysRoleVo> list = pmsRoleService.searchRoleByOrg(orgId);
            result.setContent(EnumToDataUtil.getComboData(list,"id","name",""));
        } catch (Exception e) {
            result.change(false, "获取失败,错误信息:" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysRoleController-searchRoleByOrg error: {0}", e.getMessage());
        }
        return result;
    }
}
